<?php

namespace App\Modules\Admin\Upload;

use  App\Common\Http\Basics\Upload;
use  App\Common\Http\Basics\Success;
use  App\Services\Settings\Server as SettingServer;
use  App\Dao\Mongo\FileSlice;
use  App\Utils\Oss\StsToken;
use  App\Common\Helper;
use  App\Utils\Nanoid\Control as Nanoid;
use  App\Common\Enum\OssDirPrefix;
use  App\Utils\Uuid\Control as Uuid;

class FileModule extends \App\Modules\Admin\BaseModule
{

    public function first()
    {

        $settings = SettingServer::dev()->getItem(
            ["upload_max_size", "upload_slice_size", "upload_file_type", "upload_check_file_type"]
        );

        $upload_file_type = array_map(fn ($item) => $item['type'], $settings["upload_file_type"]);
        foreach ($this->params['file_list'] as $val) {

            if ($settings['upload_check_file_type'] === 1  && !in_array($val['file_type'], $upload_file_type))  return $this->outputEnum(Upload::UPLOAD_FILE_TYPE_ERROR, ['file_name' => $val['file_name']]);

            if ($settings['upload_max_size'] * 1024 < $val['size'])  return $this->outputEnum(Upload::UPLOAD_FILE_SIZE_ERROR, ['file_name' => $val['file_name']]);
        }

        $admins_id = $this->getAdminId();
        $result = FileSlice::insert(
            $admins_id,
            $this->params['is_common'],
            [
                'dir_id'    =>    $this->params['dir_id'],       //文件夹id
                'file_size' =>    $this->params['file_size'],    //文件大小
                'admins_id' =>    $admins_id,
                'file_url'  =>    $this->params['file_url'],
                'file_type' =>    $this->params['file_type'],    //文件类型
                'file_name' =>    $this->params['file_name'],    //文件名称
            ]);
        // $file_list = [];
        // foreach ($result->getInsertedIds() as $val) $file_list[] = array('oid' => $val->__toString(), 'slice_size' =>  $settings['upload_slice_size']);
        // return $this->outputEnum(Upload::UPLOAD_FIRST_SUCCESS, ['file_list' => $file_list]);
    }

    public function followUp()
    {
        // UploadDirDao::add($this->params, $this->params['is_common'] ?  null : $this->getAdminId());
        // $admins_id = $this->getAdminId();
        // var_dump($this->params['oid']);

        // $res = FileSlice::find($admins_id, $this->params['oid']);
        // if(!$res)  return $this->outputError();
        // var_dump($res);
        // if(!$res) return  ;
        // return $this->outputSuccess([], ResultMsg::UPLOAD_SUCCESS);
    }

    public function getConfig()
    {
        $settings =  SettingServer::dev()->getItem(["upload_file_type", "upload_check_file_type", "upload_max_size", "upload_slice_size", "oss_start", "oss_access_key_id", "oss_access_key_secret", "oss_endpoint", "oss_bucket", "oss_role_arr"]);

        $upload_config = Helper::formExtractData($settings, ["upload_file_type", "upload_check_file_type", "upload_max_size", "upload_slice_size"]);



        $oss_config = [];
        if ($settings['oss_start'] === 1) {   //oss请求
            // var_dump($settings);
            $oss_config = (new StsToken(
                $settings["oss_access_key_id"],
                $settings["oss_access_key_secret"],
                $settings["oss_endpoint"],
                $settings["oss_role_arr"],
            ))->get();
            $oss_config['timeout'] = (int)$oss_config['timeout'];
            $oss_config['oss_bucket'] = $settings['oss_bucket'];
        }

        return $this->output([
            'upload' => $upload_config, 'oss' =>  $oss_config,
            'admins_id' => $this->getAdminId(),
            'dir_prefix' => [
                'common' =>  OssDirPrefix::COMMON->value,
                'admins' =>   OssDirPrefix::ADMINS->value,
            ]
        ], Success::SEL_SUCCESS);
    }


    public function stsMsg()
    {

        return $this->output(['uuid' =>   Uuid::createVersion4()] , Success::CREATE_SUCCESS);
    }
}
